/*
 * BlackballedFrame.java
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import model.ListManager;
import model.User;

/**
 * BlackballedFrame is a JFrame that displays
 * all blackballed Users.
 * 
 * @author Sean Brown
 */
@SuppressWarnings("serial")
public class BlackballedFrame extends JFrame {

	/**
	 * ListManager that contains information about the
	 * current program.
	 */
	private final ListManager my_lm;
	
	/**
	 * Constructor sets up a new Jframe and assigns values to
	 * fields.
	 * 
	 * @param the_lm The ListManager.
	 */
	public BlackballedFrame(final ListManager the_lm) {
		super("Blackballed Volunteers");
		my_lm = the_lm;
	}
	
	/**
	 * Sets up all panels, preferences, and shows the frame
	 * on screen.
	 */
	public void start() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		UIManager.put("Panel.background", Color.WHITE);
		add(userPanel());
		pack();
		setLocationRelativeTo(getRootPane());
		setVisible(true);
	}
	
	/**
	 * Returns a JPanel with buttons and all blackballed
	 * users displayed.
	 * 
	 * @return JPanel to display.
	 */
	private JPanel userPanel() {
		//Buttons
		final JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e)
            {
                dispose();
            }
        });
		
		final JPanel button_panel = new JPanel(new FlowLayout());
		button_panel.add(close);

		final JTextArea user_list = new JTextArea();
		boolean user_exists = false;
		for (User u : my_lm.getBlackballed()) {
			user_list.append(u.getName());
			user_list.append("\n");
			user_exists = true;
		}
		if (!user_exists) {
			user_list.append("No users found.");
		}
		user_list.setEditable(false);
		final JScrollPane scroller = new JScrollPane
				(user_list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		final JPanel user_panel = new JPanel(new BorderLayout());
		user_panel.add(scroller, BorderLayout.CENTER);
		user_panel.add(button_panel, BorderLayout.SOUTH);
		return user_panel;
	}
}